package com.github.abel533.autowired.lifecycle;

import com.github.abel533.C;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import static com.github.abel533.C.print;

/**
 * @author liuzh
 */
@Component
public class UserExt implements InitializingBean, DisposableBean, SmartLifecycle {

    private String name;

    private User user;

    private String osName;

    public UserExt(User user) {
        this.user = user;
        Assert.notNull(user);
        C.print("UserExt#constructor");
    }

    public String getName() {
        return name;
    }

    @Value("${autowired.testValue}")
    public void setName(String name) {
        this.name = name;
        C.print("UserExt@Value(\"${autowired.testValue}\")");
    }

    public User getUser() {
        return user;
    }

    public String getOsName() {
        return osName;
    }

    @Value("#{systemProperties['os.name']}")
    public void setOsName(String osName) {
        this.osName = osName;
        C.print("UserExt@Value(\"#{systemProperties['os.name']}\")");
    }

    public void initMethod() {
        print("@Bean(initMethod)");
    }

    public void destroyMethod() {
        print("@Bean(destroyMethod)");
    }

    @PostConstruct
    public void init() {
        print("@PostConstruct");
    }

    @PreDestroy
    public void destroy2() {
        print("@PreDestroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        print("InitializingBean#afterPropertiesSet");
    }

    @Override
    public void destroy() throws Exception {
        print("DisposableBean#destroy");
    }

    boolean running = false;

    @Override
    public void start() {
        running = true;
        print("SmartLifecycle#start");
    }

    @Override
    public void stop() {
        running = false;
        print("SmartLifecycle#stop");
    }

    @Override
    public boolean isRunning() {
        print("SmartLifecycle#isRunning");
        return running;
    }
}
